home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-06-24 | 8.1 KB | 219 lines | [TEXT/KAHL] |
- // AEGetStuff.c
-
- #include <AppleEvents.h>
- #include <AEObjects.h>
- #include <AERegistry.h>
- #include <AEPackObject.h>
- #include <Aliases.h>
- #include <Processes.h>
-
- #include "ScriptableFinder.h"
-
- #ifndef TRUE
- #define FALSE 0
- #define TRUE (!FALSE)
- #endif
-
- #include <Packages.h>
-
- #define procNotFound -1 // tmp - can't find
-
- Boolean IsRelativeProcessSerialNumber (ProcessSerialNumber *psn)
-
- /* A simple routine which determines whether the process serial number given is a relative one.
- This means that it isn't a number which refers to the process outright but rather refers to the
- process because it's current.
-
- Input: psn - the ProcessSerialNumber to check.
-
- Output: Boolean result. */
-
- {
- return((psn->highLongOfPSN == 0) && (psn->lowLongOfPSN == kCurrentProcess));
- }
-
-
- //**************************************************************************
- OSErr GetFinderProcess (ProcessSerialNumber *finderpsn, Boolean shortcut);
-
- // replace LDR's export routine
-
- OSErr GetFinderProcess (ProcessSerialNumber *finderpsn, Boolean shortcut)
-
- /* A routine to determine the process serial number of the Finder. It returns the result as a
- parameter passed by reference. It also has a Boolean parameter which specifies whether or not
- the returned process serial number will represent the Finder process serial number as
- "kCurrentProcess" if it is the current process to allow a shortcut of the _GetNextEvent call
- dependencies of processing an AppleEvent.
-
- Input: shortcut - allow "kCurrentProcess" to be used if we are in the current process.
- Input: *finderpsn - result ProcessSerialNumber passed by reference.
-
- Output: error code that occured. */
-
- {
- Boolean result;
- ProcessSerialNumber psn, currentpsn;
- ProcessInfoRec pir;
-
- psn.highLongOfPSN = 0;
- psn.lowLongOfPSN = kNoProcess;
- pir.processInfoLength = sizeof(ProcessInfoRec);
- pir.processName = nil; // don't want these bits of information
- pir.processAppSpec = nil;
- while (GetNextProcess(&psn) == noErr)
- if (GetProcessInformation(&psn, &pir) == noErr)
- if ((pir.processType == kFinderType) && (pir.processSignature == kFinderSignature))
- {
- if (shortcut && (GetCurrentProcess(¤tpsn) == noErr) &&
- (SameProcess(¤tpsn, &psn, &result) == noErr) && result)
- { // use the current process to shortcut the event dispatching
- finderpsn->highLongOfPSN = 0;
- finderpsn->lowLongOfPSN = kCurrentProcess;
- }
- else
- *finderpsn = psn; // found the Finder's psn
- return(noErr); // got the process serial number
- }
- return(procNotFound); // got an error - not found
- }
-
- //**************************************************************************
-
- OSErr GetScriptableFinderFilePos (FSSpec *f, Boolean usesystemmode, AESendMode sendmode, Boolean sendToSelf, Rect *r);
- OSErr GetScriptableFinderFilePos (FSSpec *f, Boolean usesystemmode, AESendMode sendmode, Boolean sendToSelf, Rect *r)
-
- /*
-
- Input: f - FSSpec which contains the item to the position of.
- Input: usesystemmode - a Boolean which determines whether the system's PPC port will be used.
- Input: sendmode - which mode to send the Apple Event to the Finder.
-
- Output: error code that occured. */
-
- {
- short errnum;
- OSType icondescriptor, finderSig = 'MACS';
- ProcessSerialNumber targetpsn;
- AEDesc targetaddress, propdesc, filedesc, directdesc, nulldesc, targetdesc, pointdesc;
- AppleEvent myae, myReply;
- Rect rect = {0,0,0,0};
-
-
- nulldesc.descriptorType = typeNull;
- nulldesc.dataHandle = nil;
-
- icondescriptor = pBounds;
- if (GetFinderProcess(&targetpsn, sendToSelf) == noErr)
- {
- if (usesystemmode) TurnSystemModeOn(); // you really DON'T want to do this
-
- if ((errnum = AECreateDesc(typeProcessSerialNumber, &targetpsn, sizeof(ProcessSerialNumber), &targetaddress)) == noErr) {
- if ((errnum = AECreateAppleEvent(kAECoreSuite, kAEGetData, &targetaddress, kAutoGenerateReturnID, kAnyTransactionID, &myae)) == noErr) {
- if ((errnum = AECreateDesc(typeType, &icondescriptor, sizeof(DescType), &propdesc)) == noErr) {
- AliasHandle targetAlias;
- NewAlias(nil, f, &targetAlias);
- HLock((Handle)targetAlias);
- if ((errnum = AECreateDesc(typeAlias, *targetAlias, GetHandleSize((Handle)targetAlias), &filedesc)) == noErr) {
- if ((errnum = CreateObjSpecifier(typeWildCard, &nulldesc, typeAlias,&filedesc, TRUE, &targetdesc)) == noErr) {
- if ((errnum = CreateObjSpecifier(cProperty, &targetdesc, formPropertyID, &propdesc, TRUE, &directdesc)) == noErr) {
- if ((errnum = AEPutParamDesc(&myae, keyDirectObject, &directdesc)) == noErr) {
- errnum = AESend(&myae, &myReply, sendmode, kAENeverInteract, kAEDefaultTimeout, nil, nil);
- if ( noErr) {
- // Get Rect
- if (((errnum = AEGetParamDesc(&myReply, keyAEResult, typeQDPoint, &pointdesc)) == noErr))
- rect = **((Rect **)(pointdesc.dataHandle));
- }
- }
-
- AEDisposeDesc(&directdesc);
- }
- }
- AEDisposeDesc(&filedesc);
- }
- HUnlock((Handle)targetAlias);
- AEDisposeDesc(&propdesc);
- }
- AEDisposeDesc(&myae);
- }
- AEDisposeDesc(&targetaddress);
- }
-
- // Get Rect
- *r = rect;
-
- if (usesystemmode) TurnSystemModeOff(); // we really DIDN'T want to do this
- }
- return(errnum); // the result
- }
-
-
- //---------------------------------------------------------------------------------------------
- OSErr GetScriptableFinderTrashPos (Boolean usesystemmode, AESendMode sendmode, Boolean sendToSelf, Rect *r);
- OSErr GetScriptableFinderTrashPos (Boolean usesystemmode, AESendMode sendmode, Boolean sendToSelf, Rect *r)
-
- /*
-
- Input: f - FSSpec which contains the item to the position of.
- Input: usesystemmode - a Boolean which determines whether the system's PPC port will be used.
- Input: sendmode - which mode to send the Apple Event to the Finder.
-
- Output: error code that occured. */
-
- {
- short errnum;
- OSType icondescriptor, trshdescriptor, finderSig = 'MACS';
- ProcessSerialNumber targetpsn;
- AEDesc targetaddress, propdesc, trshpropdesc, filedesc, directdesc, nulldesc, targetdesc, pointdesc;
- AppleEvent myae, myReply;
- Rect rect;
- Boolean fiddlewithA5; // See below for the usage of the A5 variables
- long saveA5;
-
-
- nulldesc.descriptorType = typeNull;
- nulldesc.dataHandle = nil;
-
- icondescriptor = pBounds;
- trshdescriptor = 'trsh';
- if (GetFinderProcess(&targetpsn, sendToSelf) == noErr)
- {
- if (usesystemmode) TurnSystemModeOn(); // you really DON'T want to do this
- fiddlewithA5 = IsRelativeProcessSerialNumber(&targetpsn);
- if (fiddlewithA5) saveA5 = SetCurrentA5();
-
- if ((errnum = AECreateDesc(typeProcessSerialNumber, &targetpsn, sizeof(ProcessSerialNumber), &targetaddress)) == noErr) {
- if ((errnum = AECreateAppleEvent(kAECoreSuite, kAEGetData, &targetaddress, kAutoGenerateReturnID, kAnyTransactionID, &myae)) == noErr) {
- if ((errnum = AECreateDesc(typeType, &icondescriptor, sizeof(DescType), &propdesc)) == noErr) {
- if ((errnum = AECreateDesc(typeType, &trshdescriptor, sizeof(DescType), &trshpropdesc)) == noErr) {
- if ((errnum = CreateObjSpecifier(cProperty, &nulldesc, formPropertyID, &trshpropdesc, TRUE, &targetdesc)) == noErr) {
- if ((errnum = CreateObjSpecifier(cProperty, &targetdesc, formPropertyID, &propdesc, TRUE, &directdesc)) == noErr) {
- if ((errnum = AEPutParamDesc(&myae, keyDirectObject, &directdesc)) == noErr) {
- errnum = AESend(&myae, &myReply, sendmode, kAENeverInteract, kAEDefaultTimeout, nil, nil);
- if ( errnum == noErr) {
- // Get Rect
- if (((errnum = AEGetParamDesc(&myReply, keyAEResult, cQDRectangle, &pointdesc)) == noErr))
- rect = **((Rect **)(pointdesc.dataHandle));
- }
- }
-
- if (directdesc.dataHandle) AEDisposeDesc(&directdesc);
- }
- }
- if (directdesc.dataHandle) AEDisposeDesc(&trshpropdesc);
- }
- if (directdesc.dataHandle) AEDisposeDesc(&propdesc);
- }
- if (directdesc.dataHandle) AEDisposeDesc(&myae);
- }
- if (directdesc.dataHandle) AEDisposeDesc(&targetaddress);
- }
-
- // Get Rect
- *r = rect;
-
- if (fiddlewithA5) SetA5(saveA5); // fix it up for crying out loud!
- if (usesystemmode) TurnSystemModeOff(); // we really DIDN'T want to do this
- }
- return(errnum); // the result
- }